from django.shortcuts import render, redirect
from django.http import HttpResponse
from ..models import Department, UserInfo, PrettyNum
from django import forms
from django.forms import Form, ModelForm
from django.utils.safestring import mark_safe
from ..utils.pagination import Pagination
from ..utils.bootstrap import BootStrapModelForm
from ..utils.form import UserModelForm, PrettynumModelForm, PrettynumEditModelForm
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError

def user_list(request):
    data_list = UserInfo.objects.all()
    page_object = Pagination(request, data_list)
    page_string = page_object.html()
    return render(request, 'user_list.html', {'data_list': page_object.page_queryset, "page_string": page_string})

def user_add(request):
    """添加用户"""
    if request.method == 'GET':
        context = {
            'gender_choices': UserInfo.gender_choices,
            'depart_list': Department.objects.all(),
        }
        return render(request, 'user_add.html', context)
    name = request.POST.get("name")
    password = request.POST.get("password")
    age = request.POST.get("salary")
    salary = request.POST.get("salary")
    create_time = request.POST.get("create_time")
    depart_id = request.POST.get("dp")
    gender = request.POST.get("gd")

    UserInfo.objects.create(name=name, password=password, age=age, salary=salary, create_time=create_time, depart_id=depart_id, gender=gender)
    return redirect('/user/list/')

#Form暂时未实现
"""
class UserAddForm(Form):
    user = forms.CharField(widget=forms.Input)
    password = forms.CharField(widget=forms.Input)
    age = forms.CharField(widget=forms.Input)
    salary = forms.CharField(widget=forms.Input)
    create_time = forms.CharField(widget=forms.Input)
    depart = forms.CharField(widget=forms.Input)
    gender = forms.CharField(widget=forms.Input)
"""
def user_form_add(request):
    """使用form组件开发/user/form/add/"""
    if request.method == 'GET':
        #form = UserAddForm()
        #return render(request, 'user_form_add.html', {"form": form})
        return render(request, 'user_form_add.html')


def user_model_form_add(request):
    if request.method == 'GET':
        form = UserModelForm()
        return render(request, 'user_model_form_add.html', {"form": form})
    # post需要进行数据校验
    form = UserModelForm(data=request.POST)
    # 内部将所有字段逐一校验
    if form.is_valid():
        form.save()   # 提交数据存储到数据表
        return redirect('/user/list/')
    return render(request, 'user_model_form_add.html', {"form": form})

def user_edit(request, nid):
    if request.method == 'GET':
        # 根据id去数据库获取要编辑的那一行数据
        row_object = UserInfo.objects.filter(id=nid).first()
        form = UserModelForm(instance=row_object)  # 加上instance  modelForm会将每一个选项的默认值选上
        return render(request, 'user_edit.html', {'form': form})
    row_object = UserInfo.objects.filter(id=nid).first()
    form = UserModelForm(data=request.POST, instance=row_object)  # instance表示你当前操作的对象，以此实现编辑功能，否则会变成新建
    if form.is_valid():
        # 默认保存用户输入的所有数据，如果想要在用户输入意外增加一点值
        # form.instance.字段名 = 值
        form.save()
        return redirect('/user/list/')
    return render(request, 'user_edit.html', {'form': form})

def user_delete(request, nid):
    UserInfo.objects.filter(id=nid).delete()
    return redirect('/user/list/')